﻿#include "mycommandlinkbutton.h"
#include <QDesktopServices>
#include <QDebug>
#include <QClipboard>
#include <QApplication>

MyCommandLinkButton::MyCommandLinkButton(QWidget *parent) : QCommandLinkButton(parent)
{
    setContextMenuPolicy(Qt::CustomContextMenu);

    QAction *actionCopy = new QAction("复制链接", this);

    menu.addAction(actionCopy);

    connect(this,&QCommandLinkButton::customContextMenuRequested,
            [=](const QPoint &point)
    {
        menu.exec(mapToGlobal(point));
    });
    connect(this,&QCommandLinkButton::clicked,this,&MyCommandLinkButton::openUrl);
    connect(actionCopy,&QAction::triggered,
            [=]()
    {
        QClipboard *clipboard = QApplication::clipboard();
        clipboard->setText(url);
    });
}

void MyCommandLinkButton::setUrl(QString url)
{
    this->url = url;
}

void MyCommandLinkButton::openUrl()
{
    QDesktopServices::openUrl(QUrl(url));
}
